<?php
class MySQL {
	public $CONN;
	
	/**
	 *	构造函数
	 *
	 */
	function __construct() {
		if(!$this->CONN)
		{
			$this->init();
		}
	}
	
	/**
	 *	初始化
	 *	@return bool
	 */
	function init() {
		global $db_config;
		$conn = mysql_connect ( $db_config ["HOST"], $db_config ["USERNAME"], $db_config ["PASSWORD"] );
		if (! $conn) {
			echo "Connection attempt failed";
			return false;
		}
		if (! mysql_select_db ( $db_config ["DB"], $conn )) {
			echo "Database Select failed";
			return false;
		}
		$this->CONN = $conn;
		mysql_query ( "set names utf8" );
		return true;
	}
	
	/**
	 *	查询
	 *	@param string $sql
	 *	@return array
	 */
	private function select($sql = "", $result_type = MYSQL_ASSOC)
	{
		$results = mysql_query ( $sql, $this->CONN);
		if ((! $results) or (empty ( $results ))) {
			return false;
		}
		$count = 0;
		$data = array ();
		while ( $row = mysql_fetch_array ( $results, $result_type ) ) {
			$data [$count] = $row;
			$count ++;
		}
		mysql_free_result ( $results );
		return $data;
	}
	
	/**
	 *	更新数据
	 *	@param string $sql
	 *	@return int
	 */
	private function update($sql = "")
	{
		$results = mysql_query ( $sql, $this->CONN);
		if ((! $results) or (empty ( $results ))) {
			return false;
		}
		return true;
	}
	
	/**
	 *	插入数据
	 *	@param string $sql
	 *	@return bool
	 */
	private function insert($sql = "")
	{
		$results = mysql_query ( $sql, $this->CONN );
		if ((! $results) or (empty ( $results ))) {
			echo mysql_error ();
			echo mysql_errno ();
			return false;
		}
		if (mysql_insert_id ()) {
			return mysql_insert_id ();
		} else {
			return true;
		}
	}
	
	/**
	 *	删除数据
	 *	@param string $sql
	 *	@return bool
	 */
	private function delete($sql = "")
	{
		$results = mysql_query ( $sql, $this->CONN);
		if ((! $results) or (empty ( $results ))) {
			return false;
		}
		return true;
	}
	
	/**
	 *	添、删、改、查统一入口
	 *	@param string $sql
	 *	@return unkown
	 */
	function query($sql = "")
	{
		if (empty ( $sql ))
		{
			return false;
		}
		if (empty ( $this->CONN ))
		{
			return false;
		}
		if(preg_match ( "/^select/i", $sql ))
		{
			return $this->select($sql);
		}
		else if (preg_match ( "/^update/i", $sql ) || preg_match ( "/^replace/i", $sql ))
		{
			return $this->update($sql);
		}
		else if (preg_match ( "/^insert/i", $sql ))
		{
			return $this->insert($sql);
		}
		else if (preg_match ( "/^delete/i", $sql ))
		{
			return $this->delete($sql);
		}
		return false;
	}
	
	/**
	 *	查询一条记录
	 *	@param string $sql
	 *	@return array
	 */
	function query_once($sql = "", $result_type = MYSQL_ASSOC)
	{
		if(!preg_match ( "/^select/i", $sql ))
		{
			return false;
		}
		$results = mysql_query ( $sql." limit 1", $this->CONN);
		if ((! $results) or (empty ( $results ))) {
			return false;
		}
		$row = mysql_fetch_array ( $results, $result_type );
		mysql_free_result ( $results );
		return $row;
	}
	
	/**
	 *	预防数据库攻击
	 *	@param string $value
	 *	@return string
	 */
	function check_col($value)
	{
		return mysql_real_escape_string ( $value, $this->CONN );
	}
	
	/**
	 *	添加一条记录
	 *	@param string $tablename 表名
	 *	@param array $insert_array 存放要存入数据的数组
	 *	@return int 新增的id值
	 */
	function insert_row($tablename, $insert_array)
	{
		if(!$tablename || !is_array($insert_array))
		{
			return false;
		}
		$keys = array();
		$vals = array();
		foreach($insert_array as $key => $val)
		{
			$keys[] = $key;
			$vals[] = $val;
		}
		unset($insert_array);
		return $this->insert("insert into ".$tablename."(".implode(',', $keys).") values ('".implode("','", $vals)."')");
	}
}
?>